# encoding: utf-8
class Admin::GroupsController < AdminController

  set_tab :permissions
  set_tab :groups, :sub_nav
  before_filter :setup, only: [:index, :show]

  respond_to :html, :json

  def index
    @groups = Group
    params[:group] ||= {}
    if params[:group].present?
      @groups.where("name like ?","%#{params[:group][:q]}%") if params[:group][:q]
      @groups.where(category: params[:group][:category]) if params[:group][:category]
    end
    @groups = @groups.all
  end

  def new
    @group = Group.new
    @groups = Group.all
  end

  def create
    sub_group_ids = params[:group].delete(:sub_ids)
    @group = Group.new(params[:group])
    if @group.save
      Group.where(id: sub_group_ids).each do |t|
        SubGroup.create(subgroup_id: t.id, group_id: @group.id)
      end
    else
      @groups = Group.all
      render :action => :new 
    end
  end

  def show
    @group = Group.find(params[:id])
    @groups = Group.where(["id != ?", @group.id]).all
    @group_permissions = @group.permissions
    @group_permissions = @group_permissions.page(params[:page])
    @ways << @group.name
  end

  def update
    sub_group_ids = params[:group].delete(:sub_ids)
    @group = Group.find(params[:id])
    if @group.update_attributes(params[:group])
      # set null for pre-group
      SubGroup.where(group_id: @group.id).each do |t|
        unless sub_group_ids.include?(t.subgroup_id.to_s)
          t.destroy
        end
      end

      Group.where(id: sub_group_ids).each do |t|
        SubGroup.where(subgroup_id: t.id, group_id: @group.id).first_or_create
      end

      redirect_to admin_group_path(@group)
      flash[:message] = 'Group was successfully updated.'
    else
      flash[:message] = 'Group was not updated due to an error.'
      @groups = Group.where(["id != ?", @group.id]).all
      @group_permissions = @group.permissions
      @group_permissions = @group_permissions.page(params[:page])
      render :action => :show
    end 
  end

  def add_permission
    @group = Group.find(params[:id])
    entities = params[:group][:entities].split(",")
    actions = params[:group][:actions].to_s.split(",")
    entities.each do |controller|
      actions.each do |action|
        permission = Permission
        permission_string = "controller:#{controller}"
        permission_string += ",action:#{action}" if not action == "*"
        permission = Permission.where(:level => params[:group][:level], :permission => permission_string).first_or_create
        if permission
          begin
            @group.permissions << permission
            flash[:message] = "Added permission: " + permission.permission
          rescue Exception
            flash[:message] = "An error occured."
          end
        else
           flash[:message] = "No permission!"
        end
      end
    end
    redirect_to admin_group_path(@group), :flash => flash
  end

  def delete_group_permission
    @group = Group.find(params[:id])
    @permission = Permission.find(params[:permission])
    if GroupsPermission.where(group_id: @group.id,permission_id: @permission.id).first.destroy
      flash[:message] = "Successfuly deleted!"
    end
    redirect_to admin_group_path(@group), :flash => flash
  end

  def destroy
    @group = Group.find(params[:id])
    begin
      @group.destroy
      flash[:message] = "组成功删除!"
    rescue
      flash[:message] = "失败！"
    end
  end

  private

  def setup
    @ways = ["权限管理","组"]
    @left_panel = "layouts/admin_left_panel"
  end

end